package com.briup.cms.web.controller;

import com.briup.cms.bean.Role;
import com.briup.cms.bean.User;
import com.briup.cms.service.IUserService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: User
 * @description:
 * @author: LiLong
 * @create: 2020-07-02
 **/
@Api(description = "测试接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public Message findAll(){
        List<User> list = userService.findAll();
        return MessageUtil.success(list);
    }

    @ApiOperation("保存或更新")
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(User user){
        userService.saveOrUpdate(user);
        return MessageUtil.success("操作成功");
    }

    @ApiOperation("根据id删除")
    @GetMapping("deleteById")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "主键", required = true,paramType = "query")
    )
    public Message deleteById(long id){
        userService.deleteById(id);
        return MessageUtil.success("删除成功");
    }
    @PostMapping("login")
    public Message login(String username, String password){
        // 虚拟token
        Map<String,String> map = new HashMap<>();
        map.put("token","admintoken");

        return MessageUtil.success(map);
    }

    @GetMapping("info")
    public Message info(){
        User user = new User();
        user.setRealname("超级管理员");
        user.setPtoto("http://47.94.139.26:8888/photo/M00/00/05/rBD-SV7-iN6Abe0NAAAXCfK1yz8375.jpg");
        return MessageUtil.success(user);
    }

    @PostMapping("logout")
    public Message logout(){
        return MessageUtil.success("退出成功");
    }
}
